const mysql = require('mysql2/promise');

const dbConfig = {
  host: process.env.DB_HOST || 'localhost',
  port: process.env.DB_PORT || 3306,
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || '',
  database: process.env.DB_NAME || 'cumrbull',
  charset: 'utf8mb4'
};

const pool = mysql.createPool(dbConfig);

const database = {
  async query(sql, params = []) {
    const [rows] = await pool.execute(sql, params);
    return rows;
  },
  
  async execute(sql, params = []) {
    const [result] = await pool.execute(sql, params);
    return result;
  },
  
  getPool() {
    return pool;
  },
  
  async close() {
    await pool.end();
  }
};

module.exports = database;
